<?php 
require_once('Class/rating.Class.php');
require_once('Class/connexionDB.Class.php');

/**
	Class used to do a calibration 
**/
class calibration
{
	var $ids = array();
	var $whereClause = '';
	/**
		filter array and add it in class
	*/
	function setValidArray($data)
	{
		$this->ids = array_filter($data);
	}

	/**
		Generate where clause to select the values
	*/
	function generateWhereClause()
	{
		if (!empty($this->ids))
		{
			$this->whereClause = "`id`=";
			$this->whereClause .= implode(' || `id`=', $this->ids);	
		}
	}

	/**
		Return rating data
	*/
	function getRatingData()
	{
		$rating = new rating();
		if (!empty($this->ids))
		{
			$this->generateWhereClause();
			$result = $rating->getRatingFromMultipleID($this->whereClause);
			return $result;
		}
		return array();
	}

	/**
		Check if the rating is from the same evaluation
	*/
	function isSameEvaluation($data)
	{
		$toCompare = array();
		foreach ($data as $key => $value)
		{
			$toCompare[] = $value['id_evaluation_form'];
		}
		$eval_id = $toCompare[0];
		foreach ($toCompare as $value)
		{
			if ($eval_id != $value)	
			{
				return false;
			}
		}
		return true;
	}

	function getCriteriaName($data)
	{
		$criteriaName = array();
		foreach ($data as $value)
		{
			if (isset($value['name_criteria']))
			{
				$criteriaName[] = array('name' => $value['name_criteria'],
										'id_criteria' => $value['original_id']);
			}
		}
		return $criteriaName;
	}

	function addCalibration($data)
	{
		$id_account		= 0;
		$nAkio		 	= '';
		$nOrder		 	= '';
		$groups		 	= '';
		$country	 	= '';
		$date_call	 	= '';
		$insertedID		= 0;
		$listedID 		= array();
		$db 			= new connexionDB();

		foreach ($data as $key => $value)
		{
			if (strpos($key, 'id_account') !== false)
			{
				$id_account = intval($value);
			}

			if (strpos($key, 'rating') !== false)
			{
				$listedID[] = $value;
			}

			if (strpos($key, 'n_akio_form') !== false)
			{
				$nAkio = $value;
			}

			if (strpos($key, 'n_order_form') !== false)
			{
				$nOrder = $value;
			}

			if (strpos($key, 'groups_form') !== false)
			{
				$groups = $value;
			}

			if (strpos($key, 'id_country_form') !== false)
			{
				$country = $value;
			}

			if (strpos($key, 'date_call_form') !== false)
			{
				$date_call = $value;
			}
		}
		$insertedID = $db->addCalibration($id_account, $nAkio, $nOrder, $groups, $country, $date_call);
		$db->linkRatingWithCalibration($listedID, $insertedID);
		return $insertedID;
	}
}


?>